var assert = require('chai').assert;
var request = require('request');
var config = require('../app/config/config.js');

// var showDebug = true;
var debug = (typeof showDebug !== 'undefined') ? console.log.bind(console) : function() {};

var httpBaseUrl = 'http://localhost:' + config.http.port + config.http.baseUrl;
suite('test api/v1/permission', function() {
	var __url = httpBaseUrl + '/v1/permission';

	function addPermission(item) {
		test('[x] create permission', function(done) {
			request({
				method: 'POST',
				url: __url,
				body: item,
				json: true,
				headers: {
					"content-type": "application/json"
				}
			}, function(err, res, body) { // body is object
				debug(body);
				assert.equal(res.statusCode, 200);
				var code = body.errCode;
				assert.isTrue(code === 0 || code === 2200); // 2200 = exists
				done();
			});
		});
	}

	var permissions = require('../app/common/all_permissions.js');
	var list = Object.keys(permissions).map(function(aName) {
		return {
			name: aName,
			code: permissions[aName][0],
			description: permissions[aName][1],
		};
	});

	for (var p of list) {
		addPermission(p);
	}
});